﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.EntityFrameworkCore;
using WpfApp1.Models;

namespace WpfApp1.ViewModels {
    public class MainViewModel: INotifyPropertyChanged {

        public ObservableCollection<CustomerViewModel> Customers { get; set; } = new();
        public ObservableCollection<DateTime> Appointment { get; set; } = new();
        private CustomerViewModel _selectedCustomer;
        private DateTime? _selectedDate;

        public DateTime? SelectedDate { 
            get=> _selectedDate;
            set { 
                if(_selectedDate != value) {
                    _selectedDate = value;
                    RaisePropertyChanged(nameof(SelectedDate));
                }
            }
        }

        public event PropertyChangedEventHandler? PropertyChanged;

        private void RaisePropertyChanged(string propertyName) {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public CustomerViewModel SelectedCustomer {
            get => _selectedCustomer;
            set  {
                if (value != _selectedCustomer) {
                    _selectedCustomer = value;
                    RaisePropertyChanged(nameof(SelectedCustomer));
                    LoadAppointments(value.Id);
                }
            }
        }

        public void LoadCustomers() {
            Customers.Clear();
            try {
                using (var dbContext = new AppDbContext()) {

                    var customers = dbContext.Customers.ToList();

                    foreach (var customer in customers) {
                        Customers.Add(new CustomerViewModel(customer));
                    }
                }
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        public void ClearSelectedCustomer() {
            _selectedCustomer = null;
            RaisePropertyChanged(nameof(SelectedCustomer));
        }

        public void SaveCustomer(string name, string idNumber, string address) {
            if(SelectedCustomer != null) {
                //更新数据
                using (var dbContext = new AppDbContext()) {
                    var customer = dbContext.Customers.Where(x => x.Id == SelectedCustomer.Id).FirstOrDefault();
                    customer.Name = name;
                    customer.IdNumber = idNumber;
                    customer.Address = address;
                    dbContext.SaveChanges();
                }
            } else {
                //添加客户
                using (var dbContext = new AppDbContext()) {
                    dbContext.Customers.Add(new Customer() { Name = name, IdNumber = idNumber, Address = address });
                    dbContext.SaveChanges();
                }
            }
            LoadCustomers();
        }

        public void LoadAppointments(int customerId) {
            Appointment.Clear();
            using (var dbContext = new AppDbContext()) {
                var appointments = dbContext.Appointments.Where(x => x.CustomerId == customerId).ToList();
                appointments.ForEach(x => Appointment.Add(x.Time));
            }
        }

        public void AddAppointment() {
            if (SelectedCustomer == null) {
                return;
            }
            using (var dbContext = new AppDbContext()) {
                var newAppointment = new Appointment() {
                    Time = SelectedDate.Value,
                    CustomerId = SelectedCustomer.Id
                };
                dbContext.Appointments.Add(newAppointment);
                dbContext.SaveChanges();
            }
            SelectedDate = null;
            LoadAppointments(SelectedCustomer.Id);
        }
    }
}
